﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Jet.Payment.Cielo.Sample
{
    public partial class Default : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void lnkCreditCardSelection_Click(object sender, EventArgs e)
        {
            var lnk = sender as LinkButton;
            var type = (Model.Types)Enum.Parse(typeof(Model.Types), lnk.CommandArgument);
            Model.ProductCielo productCielo;
            if (rdType.SelectedValue == "C")
                productCielo = Model.ProductCielo.Credito_a_vista;
            else
                productCielo = Model.ProductCielo.Debito;

            Autorize(type, productCielo);
        }

        public void Autorize(Model.Types type, Model.ProductCielo productCielo)
        {
            var returnUrl = string.Format("{0}://{1}{2}/Return.aspx",
                Request.Url.Scheme,
                Request.Url.Host,
                Request.Url.IsDefaultPort ? string.Empty : ":" + Request.Url.Port);
            /*
             *  For production config credentials
            Jet.Payment.Cielo.Config.CredentialKey = "";
            Jet.Payment.Cielo.Config.CredentialNumber = "";
            Jet.Payment.Cielo.Config.Environment = Model.Environment.Live;
            */
            var trequest = new Request.TransactionRequest();
            trequest.Authorize = Model.Authorize.Autorizar_autenticada_e_nao_autenticada;
            trequest.PaymentType = new Model.PaymentType() { Type = type, PaymentDivisionNumbers = 1, ProductCielo = productCielo };
            trequest.UrlRetorno = returnUrl;
            trequest.Order = new Jet.Payment.Cielo.Model.Order();
            trequest.Order.Number = "1033";
            trequest.Order.Description = "Servicos";
            trequest.Order.Value = 1.00M;
            var tresponse = CieloService.GetUrl(trequest);

            Session["TransactionId"] = tresponse.TransactionId;

            Response.Redirect(tresponse.Url);

        }

    }
}